#include "gitservermain.h"
#include "ui_gitservermain.h"
#include "gitservercore.h"
#include "gitdaemon.h"
#include "settings.h"
#include "settingsdialog.h"
#include <QtGui/QFileDialog>

GitServerMain::GitServerMain(QWidget *parent) :
        QMainWindow(parent), ui(new Ui::GitServerMain)
{
    ui->setupUi(this);
    m_restoreSettings();
    m_gitDaemonStatusChanged();

    connect(GitServerCore::instance()->daemon(),SIGNAL(started()),this,SLOT(m_gitDaemonStatusChanged()));
    connect(GitServerCore::instance()->daemon(),SIGNAL(stopped()),this,SLOT(m_gitDaemonStatusChanged()));
}

GitServerMain::~GitServerMain()
{
    m_saveSettings();
    delete ui;
}

//--------------------------------------------------------------------------------------------------

void GitServerMain::m_saveSettings()
{
    Settings settings;
    settings.setUiSize(size());
    settings.setUiPosition(pos());
    settings.setUiState(saveState());
    settings.setGitBasePath(ui->repoPathLineEdit->text());
    settings.setMustExportAll(ui->exportAllCheckBox->isChecked());
}

//--------------------------------------------------------------------------------------------------

void GitServerMain::m_restoreSettings()
{
    Settings settings;
    resize(settings.uiSize());
    move(settings.uiPosition());
    restoreState(settings.uiState());
    ui->repoPathLineEdit->setText(settings.gitBasePath());
    ui->exportAllCheckBox->setChecked(settings.mustExportAll());
}

//--------------------------------------------------------------------------------------------------

void GitServerMain::startDaemon()
{
    GitServerCore::instance()->daemon()->setBasePath(ui->repoPathLineEdit->text());
    GitServerCore::instance()->daemon()->setExportAll(ui->exportAllCheckBox->isChecked());
    GitServerCore::instance()->daemon()->start();
}

//--------------------------------------------------------------------------------------------------

void GitServerMain::stopDaemon()
{
    GitServerCore::instance()->daemon()->stop();
}

//--------------------------------------------------------------------------------------------------

void GitServerMain::on_startButton_clicked()
{
    startDaemon();
}

//--------------------------------------------------------------------------------------------------

void GitServerMain::on_stopButton_clicked()
{
    stopDaemon();
}

//--------------------------------------------------------------------------------------------------

void GitServerMain::m_gitDaemonStatusChanged()
{
    bool running = GitServerCore::instance()->daemon()->isRunning();
    ui->startButton->setEnabled(!running);
    ui->stopButton->setEnabled(running);

    if (running) {
        ui->statusLabel->setText("online");
        ui->imageLabel->setPixmap(QPixmap(":/images/started.tif"));
    } else {
        ui->statusLabel->setText("offline");
        ui->imageLabel->setPixmap(QPixmap(":/images/stopped.png"));
    }
}

//--------------------------------------------------------------------------------------------------

void GitServerMain::on_browseRepoBtn_clicked()
{
    QString dir = QFileDialog::getExistingDirectory(this, tr("Repository"));
    if (!dir.isEmpty())
        ui->repoPathLineEdit->setText(dir);
}

//--------------------------------------------------------------------------------------------------

void GitServerMain::on_repoPathLineEdit_textChanged(const QString & txt)
{
    if (QDir(txt).exists())
        ui->repoPathLineEdit->setStyleSheet("");
    else
        ui->repoPathLineEdit->setStyleSheet("color: red;");
}

//--------------------------------------------------------------------------------------------------

void GitServerMain::on_actionSettings_triggered()
{
    SettingsDialog *settingsDialog = GitServerCore::instance()->settingsDialog();
    settingsDialog->setParent(this);

#ifndef Q_WS_MAC
    Qt::WindowFlags flags = settingsDialog->windowFlags();
    settingsDialog->setWindowFlags(flags | Qt::Sheet);
#endif
    settingsDialog->open();
}
